package com.imooc.distribution.serialize;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.imooc.distribution.pojo.Coupon;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * 优惠券实体自定义序列化转换器
 */
public class CouponSerialize extends JsonSerializer<Coupon> {
    @Override
    public void serialize(Coupon coupon,
                          JsonGenerator generator,
                          SerializerProvider serializers) throws IOException {
        //开始序列化
        generator.writeStartObject();
        generator.writeStringField("id", coupon.getId().toString());
        generator.writeStringField("templateId", coupon.getTemplateId().toString());
        generator.writeStringField("userId", coupon.getUserId().toString());
        generator.writeStringField("couponCode", coupon.getCouponCode());
        generator.writeStringField("assignTime",
                new SimpleDateFormat("yyyy-MM-dd HH-mm-ss")
                        .format(coupon.getAssignTime()));
        generator.writeStringField("status", coupon.getStatus().getDescription());
        generator.writeStringField("name", coupon.getTemplateSDK().getName());
        generator.writeStringField("logo", coupon.getTemplateSDK().getLogo());
        generator.writeStringField("desc", coupon.getTemplateSDK().getDesc());
        //过期规则
        generator.writeStringField("expiration", JSON.toJSONString(
                coupon.getTemplateSDK().getRule().getExpiration()));
        //折扣
        generator.writeStringField("discount", JSON.toJSONString(
                coupon.getTemplateSDK().getRule().getDiscount()));
        //使用范围
        generator.writeStringField("usage", JSON.toJSONString(
                coupon.getTemplateSDK().getRule().getUsage()));
        //结束序列化
        generator.writeEndObject();
    }
}
